//
//  PayView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/23.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol PayViewDelegate {
    @objc optional func selectRepaymentType(index:Int) //选择还款方式
}

class PayView: UIView {
    
    weak var delegate : PayViewDelegate?
    
    fileprivate let cellId = "payCell"
    fileprivate let dataArray = [alipay,wechat]
    fileprivate let images = ["alipay","wechat"]

    let payButton = UIButton() //还款
    let arrowButton = UIButton() //箭头
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let whiteView = UIView()
        whiteView.backgroundColor = UIColor.white
        self.addSubview(whiteView)
        
        let redView = UIView()
        redView.backgroundColor = UIColor.red
        whiteView.addSubview(redView)
        
        let titleLabel = UILabel()
        titleLabel.text = repaymentMethod
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 18))
        whiteView.addSubview(titleLabel)
        
        arrowButton.layer.contents = UIImage.init(named: "redArrow")?.cgImage
        whiteView.addSubview(arrowButton)
        
        payButton.setTitle(repaymented, for: .normal)
        payButton.setTitleColor(UIColor.red, for: .normal)
        payButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 18))
        whiteView.addSubview(payButton)
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 0
        layout.itemSize = .init(width: ScreenW/2, height: RATIO_H(maxNum: 100))
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.clear
        collectionView.isScrollEnabled = false
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(AddBankBillCell.self, forCellWithReuseIdentifier: cellId)
        whiteView.addSubview(collectionView)
        
        whiteView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(250)
        }
        
        redView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: RATIO_H(maxNum: 20)))
            make.left.equalToSuperview().offset(RATIO(maxNum: 20))
            make.height.equalTo(RATIO_H(maxNum: 20))
            make.width.equalTo(RATIO(maxNum: 2))
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(redView.snp.right).offset(RATIO(maxNum: 10))
            make.centerY.equalTo(redView)
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        arrowButton.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -20))
            make.centerY.equalTo(titleLabel)
            make.width.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        payButton.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 60))
        }
        
        collectionView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.bottom.equalTo(payButton.snp.top).offset(RATIO_H(maxNum: -20))
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension PayView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? AddBankBillCell
        cell?.bankImageView.image = UIImage.init(named: images[indexPath.row])
        cell?.titleLabel.text = dataArray[indexPath.row]
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        self.delegate?.selectRepaymentType!(index: indexPath.row)
    }
}
